home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FILE_SEA / FILESCAN.C next >
Text File  |  1991-01-17  |  13KB  |  534 lines

  1. /*
  2.  *    
  3.  *    Clear View Systems 1990-1991
  4.  *    Clear View is Scott Roberts & Wes Zuber
  5.  *    
  6.  *    10084 Big Pine Dr.
  7.  *    Alta Loma, CA 91701
  8.  *
  9.  *    CIS 72607,3502
  10.  *    
  11.  *            
  12.  *            
  13.  *
  14.  */
  15.  
  16. #include "FileScan.h"
  17. #include "MemoryMgr.h"
  18. #include "ResourceMgr.h"
  19. #include "OSUtil.h"
  20. #include "StdFilePkg.h"
  21.  
  22.  
  23. /*
  24.     -----------------------------------------------------------------------------
  25.     Searches for the file name in theFileRec, based on the search criteria passed 
  26.     in the searchIdx parameter.
  27.     
  28.     -----------------------------------------------------------------------------
  29. */
  30. short doFileSearch(
  31.     register fileFindRec    *theFileRec,
  32.     register long            searchIdx)
  33. {
  34. register int            retval;
  35.  
  36. WDPBRec            wp;
  37. HFileInfo        cp;
  38. SysEnvRec        theWorld;
  39. char             cname[32],fileName[32];
  40.     
  41.     if (! theFileRec->fileName[0])                /* is something there? */
  42.         return(fnfErr);
  43.  
  44.     /* cname seems to be a duplicate of fileName, but cname is unstable where fileName is */
  45.     BlockMove(theFileRec->fileName,cname,32);
  46.     BlockMove(theFileRec->fileName,fileName,32);
  47.     CtoPstr(cname);
  48.     CtoPstr(fileName);
  49.  
  50.     theFileRec->ioDirID = 0L;                    /* To test later on if anything changed it */
  51.     cp.ioCompletion    = 0L;
  52.     cp.ioNamePtr    = cname;
  53.     retval        = fnfErr;    
  54.     searchIdx    &= UPPERMASK;                    /* Strip off unwanted options */
  55.     while ((retval == fnfErr) && searchIdx)
  56.         {        
  57.         /* set up cp to defaults */
  58.         cp.ioVRefNum    = 0;
  59.         cp.ioDirID        = 0L;
  60.         cp.ioFDirIndex    = 0;
  61.         
  62.         if (searchIdx & CURRENT)
  63.             {        
  64.             /* look at PMSP */
  65.             retval = PBGetCatInfo(&cp,false);
  66.             searchIdx &= ~CURRENT;
  67.             }
  68.         else if (searchIdx & BOOTDIR)
  69.             {
  70.             if (! (retval = SysEnvirons(2,&theWorld)))
  71.                 {
  72.                 cp.ioVRefNum = theWorld.sysVRefNum;
  73.                 if (searchIdx & RESTRICT)
  74.                     {
  75.                     cp.ioFDirIndex = 0;
  76.                     retval = PBGetCatInfo(&cp,false);
  77.                     }
  78.                 else
  79.                     {                
  80.                     cp.ioFDirIndex = -1;
  81.                     if (! (retval = PBGetCatInfo(&cp,false)))
  82.                         retval = scanFolder(&cp,fileName);
  83.                     }
  84.                 }
  85.             searchIdx &= ~BOOTDIR;
  86.             }
  87.         else if (searchIdx & FOLDER)
  88.             {
  89.             wp.ioCompletion = 0L;
  90.             wp.ioNamePtr    = 0L;
  91.             if (! (retval = PBHGetVol(&wp,false)))
  92.                 {
  93.                 cp.ioVRefNum    = wp.ioWDVRefNum;
  94.                 cp.ioDirID        = wp.ioWDDirID;
  95.                 if (searchIdx & RESTRICT)
  96.                     retval = PBGetCatInfo(&cp,false);
  97.                 else
  98.                     retval = scanFolder(&cp,fileName);
  99.                 }
  100.             searchIdx &= ~FOLDER;
  101.             }
  102.         else if (searchIdx & PARENT)
  103.             {
  104.             wp.ioCompletion = 0L;
  105.             wp.ioNamePtr    = 0L;
  106.             if (! (retval = PBHGetVol(&wp,false)))
  107.                 {
  108.                 cp.ioVRefNum    = wp.ioWDVRefNum;
  109.                 cp.ioDirID        = wp.ioWDDirID;
  110.                 cp.ioFDirIndex    = -1;
  111.                 if (! (retval = PBGetCatInfo(&cp,false)))
  112.                     {
  113.                     cp.ioDirID = cp.ioFlParID;
  114.                     if (searchIdx & RESTRICT)
  115.                         {
  116.                         cp.ioFDirIndex = 0;
  117.                         BlockMove(fileName,cp.ioNamePtr,32);
  118.                         retval = PBGetCatInfo(&cp,false);
  119.                         }
  120.                     else
  121.                         retval = scanFolder(&cp,fileName);
  122.                     }
  123.                 }
  124.             searchIdx &= ~PARENT;
  125.             }
  126.         else if (searchIdx & WVOLUME)
  127.             {
  128.             cp.ioDirID = 2L;
  129.             if (searchIdx & RESTRICT)
  130.                 retval = PBGetCatInfo(&cp,false);
  131.             else
  132.                 retval = scanFolder(&cp,fileName);
  133.             searchIdx &= ~WVOLUME;
  134.             }
  135.         else if (searchIdx & ALLVOLUME)
  136.             {
  137.             retval = scanAllVolumes(&cp,fileName,(searchIdx & RESTRICT)?true:false);
  138.             searchIdx &= ~ALLVOLUME;
  139.             }
  140.         else if (searchIdx & ALLWD)
  141.             {
  142.             retval = scanAllWD(&cp,fileName,(searchIdx & RESTRICT)?true:false);
  143.             searchIdx &= ~ALLWD;
  144.             }
  145.         else
  146.             searchIdx = 0L;        /* unknown search value, abandon */
  147.         }
  148.  
  149.     if (! (retval || theFileRec->ioDirID))
  150.         {
  151.         theFileRec->ioVRefNum    = cp.ioVRefNum;
  152.         theFileRec->ioDirID        = cp.ioFlParID;
  153.         }
  154.         
  155.     return(retval);
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. /*
  163.     -----------------------------------------------------------------------------
  164.     file look up stuff, use this to let the user fill in the file record.
  165.     
  166.     
  167.     
  168.     -----------------------------------------------------------------------------
  169. */
  170. short fileLook(
  171.     fileFindRec *fileFindPtr,
  172.     FileTypeRec    *fileTypes,
  173.     long        *theWDid)
  174. {
  175. volumeParam        pb1;
  176. Point            where;
  177. SFTypeList        typeList;
  178. SFReply            reply;
  179. Str255            messStr;
  180.                 
  181.     where.v=60;
  182.     where.h=80;
  183.     sprintf(messStr,"Please locate the file %s",fileFindPtr->fileName);
  184.     CtoPstr(messStr);
  185.     ParamText(messStr,"\p","\p","\p");
  186.  
  187.     if (fileTypes)
  188.         {
  189.         BlockMove(&fileTypes->fileTypes,&typeList,fileTypes->noOfTypes * sizeof(long));
  190.         SFGetFile(where,"\p",0L,fileTypes->noOfTypes,&typeList,0L,&reply);
  191.         }
  192.     else
  193.         SFGetFile(where,"\p",0L,-1,&typeList,0L,&reply);
  194.         
  195.     if (reply.good)
  196.         {
  197.         PtoCstr(reply.fName);
  198.         BlockMove(reply.fName,fileFindPtr->fileName,32);
  199.         fileFindPtr->ioVRefNum    = -(SFSaveDisk);
  200.         fileFindPtr->ioDirID    = CurDirStore;
  201.         if (theWDid)
  202.             *theWDid = reply.vRefNum;
  203.         return(noErr);
  204.         }
  205.     else    /* reply not good */
  206.         return(fnfErr);
  207.  
  208. }
  209.  
  210. /*
  211.     -----------------------------------------------------------------------------
  212.     sets the current directory and volume based on the file record passed.
  213.     this routine will need to be called before actually accessing the file.
  214.     
  215.     
  216.     -----------------------------------------------------------------------------
  217. */
  218. short fileSetDflt(
  219.     fileFindRec *fileFindPtr,
  220.     long *theWDid)
  221. {
  222. HVolumeParam    HParmBlkRec;
  223. WDPBRec            WDPBlkRec;
  224.  
  225. short            errNo;
  226.  
  227.  
  228.     memset(&WDPBlkRec,0,sizeof(WDPBRec));
  229.     WDPBlkRec.ioNamePtr = 0L;
  230.     WDPBlkRec.ioVRefNum = fileFindPtr->ioVRefNum;
  231.     WDPBlkRec.ioWDDirID = fileFindPtr->ioDirID;
  232.     WDPBlkRec.ioWDProcID = 'ERIK';
  233.     if (! (errNo = PBOpenWD(&WDPBlkRec,false)))
  234.         errNo = PBSetVol(&WDPBlkRec,false);
  235.     *theWDid = WDPBlkRec.ioVRefNum;
  236.     fileFindPtr->ioVRefNum = WDPBlkRec.ioVRefNum;
  237.     return(errNo);    
  238.  
  239. }
  240.  
  241. /*
  242.     -----------------------------------------------------------------------------
  243.     saves our file record to the current resource file.
  244.     Use this routine to save a file location. Use the complementrary routine
  245.     fileGetFromRsrc to later retreive the file record.
  246.     -----------------------------------------------------------------------------
  247. */
  248. short fileSaveToRsrc(
  249.     fileFindRec *fileFindPtr,
  250.     short        rsrcID)
  251. {
  252. fileSaveRec        theFileSave;
  253. Handle            rsrcHandle;
  254. short            errno;
  255. char            rsrcTitle[32];
  256.     
  257.     if (errno = fileFindToSave(fileFindPtr,&theFileSave))
  258.         return(errno);
  259.  
  260.     if ((errno = PtrToHand(&theFileSave,&rsrcHandle,sizeof(fileSaveRec))) || (errno = MemError()))
  261.         return(errno);
  262.     BlockMove(theFileSave.fileName,rsrcTitle,32);
  263.     CtoPstr(rsrcTitle);
  264.     AddResource(rsrcHandle,'FPTH',rsrcID,rsrcTitle);
  265.     if (! ResError())
  266.         WriteResource(rsrcHandle);
  267.  
  268.     DisposHandle(rsrcHandle);
  269.     return(ResError());    
  270. }
  271.  
  272. /*************************************************************************/
  273.  
  274. short fileGetFromRsrc(
  275.     fileFindRec *fileFindPtr,
  276.     short        rsrcID)
  277. {
  278. fileSaveRec        theFileSave;
  279. Handle            rsrcHandle;
  280. short            errno;
  281.  
  282.     rsrcHandle = GetResource('FPTH',rsrcID);
  283.     if (ResError())
  284.         return(ResError());
  285.     if (! rsrcHandle)
  286.         return(resNotFound);
  287.     
  288.     BlockMove(*rsrcHandle,&theFileSave,sizeof(fileSaveRec));
  289.     ReleaseResource(rsrcHandle);
  290.     if (errno = fileSaveToFind(&theFileSave,fileFindPtr))
  291.         return(errno);
  292.  
  293.     return(noErr);    
  294.  
  295. }
  296.  
  297. /*
  298.     -----------------------------------------------------------------------------
  299.     returns with the path in thePathStr IE DISK:HELLO:WORLD
  300.     -----------------------------------------------------------------------------
  301. */
  302. short fileFindToPath(
  303.     fileFindRec *fileFindPtr,
  304.     char *thePathStr)
  305. {
  306. register HFileInfo        cp;
  307. short    errno;
  308. char    dirName[32];
  309.     
  310.     BlockMove(fileFindPtr->fileName,thePathStr,32);
  311.     CtoPstr(thePathStr);
  312.  
  313.     cp.ioNamePtr    = (StringPtr) dirName;
  314.     cp.ioVRefNum    = fileFindPtr->ioVRefNum;
  315.     cp.ioDirID        = fileFindPtr->ioDirID;
  316.     cp.ioFDirIndex    = -1;
  317.     if (errno = PBGetCatInfo(&cp, FALSE))
  318.         return(errno);
  319.  
  320.     if (Prepend (thePathStr, cp.ioNamePtr))
  321.         return(1);
  322.     while (cp.ioDirID != 2) 
  323.         {
  324.         cp.ioDirID = cp.ioFlParID;
  325.         cp.ioFDirIndex = -1;
  326.         if (errno = PBGetCatInfo(&cp, FALSE))
  327.             return(errno);
  328.         if (Prepend (thePathStr, cp.ioNamePtr))
  329.             return(1);
  330.         }
  331.  
  332.     return(noErr);    
  333. }
  334.  
  335. /*
  336.     -----------------------------------------------------------------------------
  337.     checks to see if the file record is a valid one
  338.     false if no error, otherwise a file manager error.
  339.     -----------------------------------------------------------------------------
  340. */
  341. /*is this a valid record*/
  342.  
  343. fileTest(
  344.     fileFindRec *fileFindPtr)
  345. {
  346. HFileInfo        cp;
  347. char            fileStr[32];
  348.  
  349.     BlockMove(fileFindPtr->fileName,fileStr,32);
  350.     CtoPstr(fileStr);
  351.     cp.ioVRefNum    = fileFindPtr->ioVRefNum;
  352.     cp.ioDirID        = fileFindPtr->ioDirID;
  353.     cp.ioNamePtr    = fileStr;
  354.     cp.ioFDirIndex    = 0;    
  355.     return(PBGetCatInfo(&cp,false));
  356. }
  357.  
  358.  
  359.  
  360. /*
  361.     -----------------------------------------------------------------------------
  362. */
  363. static short scanAllVolumes(
  364.     register HFileInfo    *cp,
  365.     register Ptr        filenamePtr,
  366.     Boolean            restrict)
  367. {
  368. register int            retval;
  369. HVolumeParam     vp;
  370. char             vname[32];
  371.  
  372.  
  373.     vp.ioNamePtr = vname;
  374.     vp.ioVRefNum = 0;
  375.     
  376.     for (vp.ioVolIndex = 1; !PBGetVInfo(&vp, false); vp.ioVolIndex++)
  377.         {
  378.         cp->ioVRefNum    = vp.ioVRefNum;            
  379.         cp->ioDirID        = 2L;                            /* start at root */
  380.         if (restrict)
  381.             retval = PBGetCatInfo(cp,false);
  382.         else
  383.             retval = scanFolder(cp,filenamePtr);
  384.         if (retval != fnfErr)
  385.             break;
  386.         }
  387.     return(retval);
  388. }
  389.  
  390. /*
  391.     -----------------------------------------------------------------------------
  392. */
  393. static short scanAllWD(
  394.     register HFileInfo    *cp,
  395.     Ptr                filenamePtr,
  396.     Boolean            restrict)
  397. {
  398. register int            retval;
  399. WDPBRec            wp;
  400.     
  401.     wp.ioCompletion    = 0L;    
  402.     wp.ioNamePtr    = 0L;        
  403.     wp.ioVRefNum    = 0;                /* Look at all volumes */    
  404.     wp.ioWDProcID    = 0l;                /* Look at all working directories */
  405.     
  406.     for (wp.ioWDIndex = 1; !PBGetWDInfo(&wp, false); wp.ioWDIndex++)
  407.         {
  408.         cp->ioVRefNum    = wp.ioVRefNum;
  409.         cp->ioDirID        = wp.ioWDDirID;
  410.         if (restrict)
  411.             retval = PBGetCatInfo(cp,false);
  412.         else
  413.             retval = scanFolder(cp,filenamePtr);
  414.         if (retval != fnfErr)
  415.             break;
  416.         wp.ioVRefNum = 0;        
  417.         }
  418.     return(retval);
  419. }
  420.  
  421. /*
  422.     -----------------------------------------------------------------------------
  423. */
  424.  
  425. static short fileFindToSave(
  426.     fileFindRec *fileFindPtr,
  427.     fileSaveRec *fileSavePtr)
  428. {
  429. HVolumeParam    HParmBlkRec;
  430. short            errno;
  431.  
  432.     memset(&HParmBlkRec,0,sizeof(HVolumeParam));
  433.     memset(fileSavePtr,0,sizeof(fileSaveRec));
  434.     BlockMove(fileFindPtr->fileName,fileSavePtr->fileName,32);
  435.     fileSavePtr->ioDirID = fileFindPtr->ioDirID;
  436.     HParmBlkRec.ioNamePtr = fileSavePtr->volName;
  437.     HParmBlkRec.ioVRefNum = fileFindPtr->ioVRefNum;
  438.     errno = PBHGetVInfo(&HParmBlkRec,false);
  439.     PtoCstr(fileSavePtr->volName);
  440.     return(errno);    
  441.  
  442. }
  443.  
  444. /*
  445.     -----------------------------------------------------------------------------
  446. */
  447. static short fileSaveToFind(
  448.     fileSaveRec *fileSavePtr,
  449.     fileFindRec *fileFindPtr)
  450. {
  451. HVolumeParam    HParmBlkRec;
  452. short            errno;
  453. char            volName[32];
  454.  
  455.     memset(&HParmBlkRec,0,sizeof(HVolumeParam));
  456.     memset(fileFindPtr,0,sizeof(fileFindRec));
  457.     BlockMove(fileSavePtr->fileName,fileFindPtr->fileName,32);
  458.     fileFindPtr->ioDirID = fileSavePtr->ioDirID;
  459.     CtoPstr(fileSavePtr->volName);
  460.     BlockMove(fileSavePtr->volName,volName,32);
  461.     volName[0]++;
  462.     volName[volName[0]] = ':';
  463.     HParmBlkRec.ioNamePtr = volName;
  464.     HParmBlkRec.ioVolIndex = -1;
  465.     errno = PBHGetVInfo(&HParmBlkRec,false);
  466.     fileFindPtr->ioVRefNum = HParmBlkRec.ioVRefNum;
  467.     return(errno);    
  468.  
  469. }
  470.  
  471. /*
  472.     -----------------------------------------------------------------------------
  473. */
  474. static short Prepend( s1, s2 )
  475. register char s1[], s2[];
  476. {
  477.     register int i,j;
  478.     
  479.     i = s1[0];
  480.     j = s2[0];
  481.     if ((i+j)>255)
  482.         return(1);                    /* Our path is longer than 255 */
  483.     s1[0] = i+j;
  484.     if (i>0) 
  485.         {
  486.         j++;
  487.         do s1[i+j] = s1[i]; while (--i);
  488.         s1[j--] = ':';
  489.         s1[0]++;
  490.         }
  491.     do s1[j] = s2[j]; while (--j);
  492.  
  493.     return(noErr);
  494. }
  495.  
  496. /*
  497.     -----------------------------------------------------------------------------
  498. */
  499. static short scanFolder(
  500.     register HFileInfo    *myCPB,
  501.     register Ptr        filenamePtr)    /* a PString */
  502. {
  503. register OSErr    retval = 0;
  504. register long    dirIDToSearch = myCPB->ioDirID;
  505. register short    index=1;            /* for ioFDirIndex */
  506.  
  507.     do
  508.         {
  509.         myCPB->ioFDirIndex = index;                /* set up the index */
  510.         myCPB->ioDirID = dirIDToSearch;            /* we need to do this every time through, since GetCatInfo returns ioFlNum in this field*/    
  511.         retval = PBGetCatInfo(myCPB,false);
  512.         if (retval == noErr) 
  513.             {
  514.             /* check the file attributes for folderhood */
  515.             if (myCPB->ioFlAttrib & fIsDir)
  516.                 { /*we have a dir*/
  517.                 if (retval = scanFolder(myCPB,filenamePtr))        /* Process this folder */                
  518.                     retval = noErr;                /* reset the error message */
  519.                 else
  520.                     break;
  521.                 } /* end if myCPB->ioFlAttrib */
  522.             else 
  523.                 { /*we have a file*/
  524.                 if (EqualString(myCPB->ioNamePtr,filenamePtr,false,false))
  525.                     break;
  526.                 } /* end else*/
  527.             index++;                            /* increment the index for GetCatInfo */
  528.             } /* end if (retval == noErr) */
  529.         } while (retval == noErr);
  530.  
  531.     return(retval);
  532. }
  533.  
  534.